package com.cyy.AI_mbti.scoring;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cyy.AI_mbti.model.dto.question.QuestionContentDTO;
import com.cyy.AI_mbti.model.entity.App;
import com.cyy.AI_mbti.model.entity.Question;
import com.cyy.AI_mbti.model.entity.ScoringResult;
import com.cyy.AI_mbti.model.entity.UserAnswer;
import com.cyy.AI_mbti.service.QuestionService;
import com.cyy.AI_mbti.service.ScoringResultService;
import lombok.RequiredArgsConstructor;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RequiredArgsConstructor
@ScoringStrategyConfig(appType = 0, scoringStrategy = 0)
public class ScoreScoreScoringStrategyImpl implements ScoringStrategy {
    private  final QuestionService questionService;
    private final ScoringResultService scoringResultService;
    @Override
    public UserAnswer doScore(List<String> choices, App app) throws Exception {
        Long appId = app.getId();
        //Db.lambdaQuery(Question.class);
        Question question = questionService.getOne(
                Wrappers.lambdaQuery(Question.class)
                        .eq(Question::getAppId, app.getId()));

        List<ScoringResult> scoringResultList = scoringResultService.list(
                Wrappers.lambdaQuery(ScoringResult.class)
                        .eq(ScoringResult::getAppId, app.getId()).orderByDesc(ScoringResult::getResultScoreRange));
        int sum=0;
        List<QuestionContentDTO> questionDTOS = JSONUtil.toList(question.getQuestionContent(), QuestionContentDTO.class);
        for (int i=0;i<choices.size();i++) {
            String answer=choices.get(i);
            List<QuestionContentDTO.Option> options = questionDTOS.get(i).getOptions();
            for(QuestionContentDTO.Option option : options){
                if (option.getKey().equals(answer)) {
                    sum+=option.getScore();
                }
            }
        }
        ScoringResult maxScoringResult = scoringResultList.get(0);
        // 遍历评分结果列表
        for (ScoringResult scoringResult : scoringResultList) {
            // 如果分数高于当前最高分数，更新最高分数和最高分数对应的评分结果
            if (sum >= scoringResult.getResultScoreRange()) {
                maxScoringResult = scoringResult;
                break;
            }
        }

        // 4. 构造返回值，填充答案对象的属性
        UserAnswer userAnswer = new UserAnswer();
        userAnswer.setAppId(appId);
        userAnswer.setAppType(app.getAppType());
        userAnswer.setScoringStrategy(app.getScoringStrategy());
        userAnswer.setChoices(JSONUtil.toJsonStr(choices));
        userAnswer.setResultId(maxScoringResult.getId());
        userAnswer.setResultName(maxScoringResult.getResultName());
        userAnswer.setResultDesc(maxScoringResult.getResultDesc());
        userAnswer.setResultPicture(maxScoringResult.getResultPicture());
        userAnswer.setResultScore(sum);
        return userAnswer;
    }
}
